/*
    RoleCall provides a web service that web applications can use to find 
    the roles associated with a user that has been authenticated via OpenID. 
    Also provides a web-based user interface to manage the user roles 
    associated with partner web applications.
    
    Development of this software was supported in part by the David and Lucile 
    Packard Foundation and by the the Office Of Naval Research (ONR) 
     
    Copyright (c) 2012, 
    Monterey Bay Aquarium Research Institute - MBARI ( www.mbari.org )
    Michael Godin ( mikegodin AT users DOT sourceforge DOT net )

    RoleCall is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    RoleCall is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar (see the file COPYING.GPL).  If not, see 
    <http://www.gnu.org/licenses/>.  
*/

package rolecall.js.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.mbari.rolecall.server.PersistentPropertyStore;

public class Props {

    private static Logger logger = Logger.getLogger(Props.class.getPackage()
            .getName() + "." + Props.class.getName());

    static protected Properties properties = new Properties();

    private static final PersistentPropertyStore propertyStore = new PersistentPropertyStore() {
        @Override
        public String get(String key, String defaultValue) {
            String value = Props.get(key);
            if (null == value || value.isEmpty()) {
                value = defaultValue;
            }
            return value;
        }

        @Override
        public void set(String key, String value) {
            Props.set(key, value);
        }
    };

    static protected File propFile;

    static {
        load();
    }

    public static String get(String key) {
        return properties.getProperty(key);
    }

    /**
     * @return the propertyStore
     */
    public static PersistentPropertyStore getPropertyStore() {
        return propertyStore;
    }

    static protected void load() {
        InputStream inStream = null;
        try {
            inStream = Props.class.getClassLoader().getResourceAsStream(
                    "default.properties");
            properties.load(inStream);
            inStream.close();
            URL defaultPropURL = Props.class.getClassLoader().getResource(
                    "default.properties");
            File defaultPropFile = new File(URLDecoder.decode(
                    defaultPropURL.getFile(), "UTF-8"));
            String webappFilename = defaultPropFile.getParentFile()
                    .getParentFile().getParent();
            propFile = new File(webappFilename + ".properties");
            if (propFile.exists()) {
                inStream = new FileInputStream(propFile);
                if (null != inStream) {
                    properties.load(inStream);
                    inStream.close();
                }
            }
        } catch (IOException ioe) {
            logger.log(Level.SEVERE, "Error loading properties", ioe);
        }
    }

    public static boolean remove(String key) {
        properties.remove(key);
        return store();
    }

    public static boolean set(String key, String value) {
        properties.setProperty(key, value);
        return store();
    }

    static boolean store() {
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(propFile);
            properties.store(fos, null);
            fos.close();
        } catch (FileNotFoundException e) {
            logger.log(Level.SEVERE,
                    "Could not open properties file for saving.", e);
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Could not save properties file.", e);
        }
        return true;
    }
}
